package com.lili.collection.core.serializer;

import cn.hutool.core.lang.Singleton;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.filter.Filter;

/**
 * @author lili
 * @description TODO
 * @date 2023/2/28 5:10
 */
public class FastjsonSerializer {
    public String serialize(Object instance) {
        return JSON.toJSONString(instance,
                new Filter[]{Singleton.get(FieldDefPropertyPreFilter.class), Singleton.get(FieldDefPropertyValueFilter.class)},
                JSONWriter.Feature.WriteNulls,
                JSONWriter.Feature.WriteClassName,
                JSONWriter.Feature.WriteEnumsUsingName,
                JSONWriter.Feature.WriteNullListAsEmpty);
    }

    /**
     * 反序列化配置
     */
    public Object deserialize(String value) {
        return JSON.parse(value.toString(),
                JSONReader.Feature.SupportAutoType);
    }
}
